﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using System.Net;
using System.IO;

namespace MovieTweet
{
	public class YouTubeInfo
	{
		public string getJsonData(string url)
		{
			HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
			request.Method = "GET";
			WebResponse response = request.GetResponse();
			StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
			return reader.ReadToEnd();
		}
		
		public string getVideoURL(string movieTitle)
		{
			string baseUrl = "http://gdata.youtube.com/feeds/api/videos";
			string url = baseUrl + "?q= " + HttpUtility.UrlEncode(movieTitle) + "&max-result=1&category=trailer&alt=json";
			string jsonData = getJsonData(url);
			JavaScriptSerializer jSerialize = new JavaScriptSerializer();
			YouTubeResult result = jSerialize.Deserialize<YouTubeResult>(jsonData);
			return result.feed.entry[0].link[0].href;
		}
	}

	public class YouTubeResult
	{
		public Feed feed;
	}

	public class Feed
	{
		public List<Entry> entry;
	}

	public class Entry
	{
		public List<Link> link;
	}

	public class Link
	{
		public string href;
	}
}
